<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {

	function __construct(){
		parent::__construct();
		go_admin();
		$this->load->library('form_validation');
	}

	public function index(){
		$this->load->model('person_model');
		$data['lists'] = $this->person_model->getLists();
		$this->load->view('admin/lists.tpl',$data);
	}

	public function person(){
		$data['error'] = '';
		$data['success'] = '';
		$this->form_validation->set_rules('name', 'name', 'trim|xss_clean|required');
		$this->form_validation->set_rules('website', 'website', 'trim|xss_clean|required');
		$this->form_validation->set_rules('avatar', 'avatar', 'trim|xss_clean');
		$this->form_validation->set_rules('office', 'office', 'trim|xss_clean');
		$this->form_validation->set_rules('address', 'address', 'trim|xss_clean');
		$this->form_validation->set_rules('email', 'email', 'trim|xss_clean');
		$this->form_validation->set_rules('brif', 'brif', 'trim|xss_clean');
		$this->form_validation->set_rules('introduction', 'introduction', 'trim|xss_clean');
		$this->form_validation->set_rules('country', 'country', 'trim|xss_clean');
		$this->form_validation->set_rules('phone', 'phone', 'trim|xss_clean');
		$this->form_validation->set_rules('fax', 'fax', 'trim|xss_clean');
		$this->form_validation->set_rules('tag', 'tag', 'trim|xss_clean');
		$this->load->model('person_model');
		if($this->form_validation->run()){
			if($this->person_model->addPerson()){
				redirect('admin/success');
			}else{
				$data['error'] = 'oops! some errors occur!';	
			}
		}
		$id = (int)$this->uri->segment(3);
		if($id){
			$data['person'] = $this->person_model->getPersonByID($id);
			$data['action'] = '/admin/update/' . $id;
			$data['button'] = 'Update';
			$this->load->view('admin/edit.tpl',$data);
		}else{
			$data['person'] = false;
			$data['action'] = '/admin/person';
			$data['button'] = 'Add';
			$this->load->view('admin/form.tpl',$data);
		}
	}
	
	public function success(){
		$this->load->view('admin/success.tpl');
	}

	public function lists(){
		$this->load->view('admin/lists.tpl');
	}

	public function update(){
		$this->load->model('person_model');
		$id = (int)$this->uri->segment(3);
		if($id){
			$data['error'] = '';
			$data['success'] = '';
			$this->form_validation->set_rules('name', 'name', 'trim|xss_clean|required');
			$this->form_validation->set_rules('website', 'website', 'trim|xss_clean|required');
			$this->form_validation->set_rules('avatar', 'avatar', 'trim|xss_clean');
			$this->form_validation->set_rules('office', 'office', 'trim|xss_clean');
			$this->form_validation->set_rules('address', 'address', 'trim|xss_clean');
			$this->form_validation->set_rules('email', 'email', 'trim|xss_clean');
			$this->form_validation->set_rules('brif', 'brif', 'trim|xss_clean');
			$this->form_validation->set_rules('introduction', 'introduction', 'trim|xss_clean');
			$this->form_validation->set_rules('country', 'country', 'trim|xss_clean');
			$this->form_validation->set_rules('phone', 'phone', 'trim|xss_clean');
			$this->form_validation->set_rules('fax', 'fax', 'trim|xss_clean');
			$this->form_validation->set_rules('tag', 'tag', 'trim|xss_clean');
			$this->load->model('person_model');
			if($this->form_validation->run()){
				if($this->person_model->update($id)){
					redirect('admin/success');
				}else{
					$data['error'] = 'oops! some errors occur!';	
					$data['person'] = $this->person_model->getPersonByID($id);
					$data['action'] = '/admin/update';
					$data['button'] = 'Update';
					$this->load->view('admin/edit.tpl',$data);
				}
			}
		}else{
			show_error('参数不正确！');
		}
	}
	public function picsave(){
		$size = @getimagesize($_FILES['uploadfile']['tmp_name']);
		$exit = explode('/',$size['mime']);
		if($exit[1] == 'x-ms-bmp') $exit[1] = 'bmp';
		$filepath = 'photo' . DS .date("Y"). DS .date("m"). DS.date("d");
		$fullpath = BASEDIR . DS . $filepath;
		if(is_dir($fullpath)==false){
			@mkdir($fullpath,0700,1);
		}
		$photourl = date('His') .'_' . $this->randomkeys(5). '.' . $exit[1];
		$filesrc = $filepath . DS .$photourl;
		$filename = $_FILES['uploadfile']['name'];
		 move_uploaded_file($_FILES['uploadfile']['tmp_name'], BASEDIR . DS . $filesrc);
		 $result['src'] = $return['src'] = $filesrc;
			$this->load->library('picturethumb');
		 $this->picturethumb->image(BASEDIR.DS.$filepath.DS.$photourl,$photourl);
		 $path = str_replace('\\','/',$filesrc);
		 echo json_encode(array('path'=>HTTP_IMAGE . $path,'value'=>$path));
		 exit;
	}
	function randomkeys($length){
		$key='';
		$pattern = "1234567890abcdefghijklmnopqrstuvwxyz";
		for($i=0;$i<$length;$i++){
			$key .= $pattern{rand(0,35)};
		}
		return $key;
   }


   public function delete($id){
		$id = (int)$this->uri->segment(3);
		if($id){
			$this->load->model('person_model');
			$this->person_model->del($id);
			redirect('/admin');
		}else{
			show_404();
		}
   }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */